package com.cnunicom.modules.tra.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cnunicom.common.utils.Constant;
import com.cnunicom.common.utils.PageUtils;
import com.cnunicom.common.utils.Query;
import com.cnunicom.modules.tra.dao.HistoryDao;
import com.cnunicom.modules.tra.entity.HistoryEntity;
import com.cnunicom.modules.tra.service.HistoryService;
import com.cnunicom.modules.tra.vo.HisMebUnitVO;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;


@Service("historyService")
public class HistoryServiceImpl extends ServiceImpl<HistoryDao, HistoryEntity> implements HistoryService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        String mebName = (String) params.get("mebName");
        String roomType = (String) params.get("roomType");
        String unitName = (String) params.get("unitName");
        String checkinTime = (String) params.get("checkinTime");

        IPage<HistoryEntity> page = this.page(
                new Query<HistoryEntity>().getPage(params),
                new QueryWrapper<HistoryEntity>()
                        .like(StringUtils.isNotBlank(mebName), "def2", mebName)
                        .like(StringUtils.isNotBlank(roomType), "def1", roomType)
                        .like(StringUtils.isNotBlank(unitName), "def0", unitName)
                        .eq(StringUtils.isNotBlank(checkinTime), "checkin_time", checkinTime)
        );

        return new PageUtils(page);
    }

    /**
     * 蛇皮而且不优秀的分页关联多表查询
     *
     * @param params
     * @return
     */
    @Override
    public PageUtils queryHisMemUnitPage(Map<String, Object> params) {


        HisMebUnitVO vo = voHandle(params);

        IPage<HisMebUnitVO> page = new Query<HisMebUnitVO>().getPage(params,"create_time", false);
        List<HisMebUnitVO> vos = this.baseMapper.queryHisMemUnitPage((Page) params.get(Constant.PAGE), vo);
        page.setRecords(vos);
        return new PageUtils(page);
    }

    /**
     * 记录表查询用
     * @param vo
     * @return
     */
    @Override
    public List<HisMebUnitVO> queryHisMemUnit(HisMebUnitVO vo) {
        return this.baseMapper.queryHisMemUnitPage(null, vo);
    }

    @Override
    public HisMebUnitVO getById(Integer id) {
        return this.baseMapper.getById(id);
    }

    private HisMebUnitVO voHandle(Map<String, Object> params) {

        HisMebUnitVO vo = new HisMebUnitVO();


        String superiorName = (String)params.get("superiorName");
        String floorRoomName = (String)params.get("floorRoomName");
        String mebName = (String)params.get("mebName");
        String checkinTime = (String)params.get("checkinTime");

        vo.setSuperiorName(superiorName);
        vo.setFloorRoomName(floorRoomName);
        vo.setMebName(mebName);
        vo.setCheckinTime(checkinTime);

        return vo;
    }

}
